package com.kk.xhr.admin.controller;

import com.kk.xhr.admin.biz.IContractBizService;
import com.kk.xhr.admin.controller.request.ContractAddRequest;
import com.kk.xhr.admin.controller.request.ContractModifyRequest;
import com.kk.xhr.admin.controller.request.ContractQueryRequest;
import com.kk.xhr.admin.controller.vo.ContractVO;
import com.kk.xhr.common.pojo.BinaryEnum;
import com.kk.xhr.web.vo.ApiResult;
import com.kk.xhr.web.vo.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;

/**
 * 合同管理控制器
 *
 * @author luokexiong
 * @version 1.0 2021/2/19
 * @since 1.0.0
 */
@RestController
@RequiredArgsConstructor
@Api(value = "合同管理", tags = "合同管理")
@RequestMapping("/contract")
public class ContractController {
    private final Executor adminControllerBizExecutor;
    private final IContractBizService contractBizService;

    @ApiOperation(value = "分页查询合同")
    @GetMapping
    @RequiresRoles(value = {"admin"})
    public CompletionStage<ApiResult<PageVO<ContractVO>>> queryPage(@Valid ContractQueryRequest request) {
        return CompletableFuture.supplyAsync(() -> ApiResult.success(contractBizService.queryPage(request)), adminControllerBizExecutor);
    }

    @ApiOperation(value = "添加合同")
    @PostMapping
    @RequiresRoles(value = {"admin"})
    public CompletionStage<ApiResult<Void>> add(@RequestBody @Valid ContractAddRequest request) {
        return CompletableFuture.supplyAsync(() -> {
            contractBizService.add(request);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "修改合同")
    @PutMapping("/{id:.+}")
    @RequiresRoles(value = {"admin"})
    public CompletionStage<ApiResult<Void>> modify(@PathVariable("id") Long id, @RequestBody @Valid ContractModifyRequest request) {
        return CompletableFuture.supplyAsync(() -> {
            contractBizService.modify(id, request);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "修改合同状态")
    @PutMapping("/status/{id:.+}")
    @RequiresRoles(value = {"admin"})
    public CompletionStage<ApiResult<Void>> modifyStatus(@PathVariable("id") Long id, BinaryEnum status) {
        return CompletableFuture.supplyAsync(() -> {
            contractBizService.modifyStatus(id, status);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }
}
